//
//  Timer+Ex.swift
//  aquariumstudio
//
//  Created by zj on 2022/10/19.
//  Copyright © 2022 LXF. All rights reserved.
//

import Foundation

public extension Timer{
    
    /// 相对安全的 定时器, 弱引用 object,
    static func start<TObject: AnyObject>(mountTo object:TObject,timeInterval:TimeInterval,tickCallback:@escaping (Timer,TObject)->())->Timer{
        
        let t = Timer.init(timeInterval: timeInterval, repeats: true) { [weak object] time in
//            log.w("tick")
            guard let object = object else {
//                log.w("tick invalidate")
                time.invalidate()
                return
            }
//            log.w("tick ok")
            tickCallback(time,object)
        }
        RunLoop.current.add(t, forMode: RunLoop.Mode.common)
        return t
    }
    
}
